/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterceptorPanel.java
 *
 * Created on Aug 27, 2010, 5:28:59 PM
 */
package org.apache.directory.http.client.ui;


import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.directory.http.client.data.model.EntryData;
import org.apache.directory.http.client.ui.callback.ConfigAware;
import org.apache.directory.http.client.ui.callback.ConfigModificationHandler;
import org.apache.directory.http.client.ui.renderer.EntryDataCellRenderer;


/**
 *
 * @author kayyagari
 */
public class InterceptorPanel extends javax.swing.JPanel implements ConfigModificationHandler, ConfigAware,
    ListSelectionListener
{

    private List<EntryData> data;


    /** Creates new form InterceptorPanel */
    public InterceptorPanel()
    {
        initComponents();
        ApplyPanel panel = new ApplyPanel();
        panel.setModificationHandler( this );
        applyPanel.add( panel );
        lstInterceptors.addListSelectionListener( this );
    }


    public void cancel()
    {
    }


    public void apply()
    {
    }


    public void setEntryData( List<EntryData> data )
    {
        this.data = data;
        lstInterceptors.setModel( new EntryDataModel() );
        lstInterceptors.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        lstInterceptors.setCellRenderer( new EntryDataCellRenderer( "ads-interceptorClassName" ) );
    }


    public void valueChanged( ListSelectionEvent e )
    {
        if ( e.getValueIsAdjusting() )
        {
            return;
        }

        int index = lstInterceptors.getSelectedIndex();
        if ( index == -1 )
        {
            return;
        }

        EntryData selected = ( EntryData ) lstInterceptors.getSelectedValue();
        try
        {
            txtId.setText( selected.getString( "ads-interceptorId" ) );
            if ( selected.exists() )
            {
                txtId.setEditable( false );
            }
            
            txtClass.setText( selected.getString( "ads-interceptorClassName" ) );
            
            String val = selected.getString( "ads-enabled" );
            if( val == null )
            {
                val = "true";
            }
            
            chbEnable.setSelected( Boolean.parseBoolean( val ) );
        }
        catch ( Exception ex )
        {
            //shouldn't happen
            ex.printStackTrace();
        }
    }

    class EntryDataModel extends DefaultListModel
    {

        @Override
        public Object getElementAt( int index )
        {
            return data.get( index );
        }


        @Override
        public int getSize()
        {
            if ( data == null || data.isEmpty() )
            {
                return 0;
            }

            return data.size();
        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstInterceptors = new javax.swing.JList();
        lblId = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        chbEnable = new javax.swing.JCheckBox();
        lblClass = new javax.swing.JLabel();
        txtClass = new javax.swing.JTextField();
        btnUp = new javax.swing.JButton();
        btnDown = new javax.swing.JButton();
        applyPanel = new javax.swing.JPanel();

        jScrollPane1.setViewportView(lstInterceptors);

        lblId.setText("Id: ");

        chbEnable.setText("Enable");

        lblClass.setText("Class:");

        btnUp.setText("Up");
        btnUp.setEnabled(false);

        btnDown.setText("Down");
        btnDown.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(11, 11, 11)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 215, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnUp, 0, 0, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnDown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(41, 41, 41)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(20, 20, 20)
                                .add(lblId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(lblClass))
                        .add(2, 2, 2)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(txtId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 179, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(chbEnable))
                            .add(txtClass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 324, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(94, 94, 94)
                        .add(applyPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 308, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(107, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(88, 88, 88)
                            .add(btnUp)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(btnDown))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(applyPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(31, 31, 31)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblId)
                            .add(chbEnable)
                            .add(txtId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblClass)
                            .add(txtClass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 258, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel applyPanel;
    private javax.swing.JButton btnDown;
    private javax.swing.JButton btnUp;
    private javax.swing.JCheckBox chbEnable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblClass;
    private javax.swing.JLabel lblId;
    private javax.swing.JList lstInterceptors;
    private javax.swing.JTextField txtClass;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables

}
